// pages/hardware_management/hardware_association/materials.js
const myBehavior = require("../../../behaviors/requests")

Page({

  /**
   * 页面的初始数据
   */
  behaviors: [myBehavior],
  data: {
    base_urlstr: "https://logisticsengineering.gree.com/order_system",
    login_show: false,
    user_email: "661725",
    password: "GHS123456*",
    token: "",
    company_datas: {},
    company_options: [],
    company_value: "",
    manager_value: "",
    manager_options: [],
    popup_company_show: false,
    popup_manager_show: false,
    queryInfo: {
      page: 1,
      page_size: 99,
    },
    label_value: "",
    label_list: [],
    popup_label_show: false,
    position_value: "",
    position_list: [],
    popup_position_show: false,
    indicator_value: "",
    indicator_list: [],
    popup_indicator_show: false,
    pad_value: "",
    pad_list: [],
    popup_pad_show: false,
  },

  // 登录弹框确定按钮
  onConfirm() {
    var url = this.data.base_urlstr + "/login";
    var data = {
      user_email: this.data.user_email,
      password: this.data.password
    }
    wx.request({
      url,
      data,
      dataType: "json",
      method: "POST",
      success: (res) => {
        var result = res.data
        // console.log(result)

        if (result.code == 20000) {
          this.setData({
            login_show: false,
            token: result.data.token

          })
          wx.showToast({
            title: result.msg,
            icon: "success"
          });
          this.getPermission()
        } else {
          this.setData({
            login_show: true,
          })
          wx.showToast({
            title: result.msg,
            icon: "error"
          });
        }
      },
      fail(res) {
        console.log(res);
        const result = {
          code: "50010",
          msg: "服务器出现错误！",
          data: {}
        }
        reject(result);
      }
    });


  },
  // 获取公司信息
  getPermission() {
    var url = this.data.base_urlstr + "/get_u_permission";
    var data = {}
    var token = this.data.token
    wx.request({
      url,
      data,
      header: {
        "Authorization": token
      },
      dataType: "json",
      method: "POST",
      success: (res) => {
        var result = res.data
        // console.log("公司信息", result)

        if (result.code == 20000) {

          this.data.company_datas = result.data
          let company_name_list = []
          for (let i in result.data) {
            if (result.data.hasOwnProperty(i))
              company_name_list.push(i)
          }
          this.setData({
            company_datas: result.data,
            company_options: company_name_list,

          })
        } else {
          wx.showToast({
            title: result.msg,
            icon: "error"
          });
        }
      },
      fail(res) {
        console.log(res);
        const result = {
          code: "50010",
          msg: "服务器出现错误！",
          data: {}
        }
        reject(result);
      }
    });
  },
  // 选择公司输入框
  onCompanyClick(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_company_show: true
    })
  },
  // 选择公司输入框关闭
  onCompanyClose() {
    this.setData({ popup_company_show: false });
  },
  // 选择完公司输入框关闭
  onCompanyPopupChange(event) {
    this.setData({
      manager_value: "",
    })
    const { picker, value, index } = event.detail;
    var value_datas = {}
    value_datas = this.data.company_datas
    var key_value_list = []
    key_value_list = value_datas[value]
    this.setData({
      company_value: value,
      manager_options: key_value_list,
      popup_company_show: false,

    })
  },
  // 选择单位输入框
  onManagerClick(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_manager_show: true
    })
  },
  // 选择单位输入框关闭
  onManagerClose() {
    this.setData({ popup_manager_show: false });
  },
  // 选择完单位输入框关闭
  onManagerPopupChange(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      manager_value: value,
      popup_manager_show: false,
      label_value: "",
      position_value: "",
      indicator_value: "",
      pad_value: "",
    })
    this.getLabelDatas()
    this.getPositionDatas()
    this.getIndicatorDatas()
    this.getPadDatas()
  },
  // 查询亮灯标签
  getLabelDatas() {
    var url = this.data.base_urlstr + "/query_source";
    var data = {
      page: this.data.queryInfo.page,
      page_size: this.data.queryInfo.page_size,
      company: this.data.company_value,
      manager: this.data.manager_value,
      mac: "",
      supply: "",
      register_time: []

    }
    var token = this.data.token
    wx.request({
      url,
      data,
      header: {
        "Authorization": token
      },
      dataType: "json",
      method: "POST",
      success: (res) => {
        var result = res.data
        console.log("标签信息", result)

        if (result.code == 20000) {
          this.setData({
            label_list: result.data.results,
          })
        } else {
          wx.showToast({
            title: result.msg,
            icon: "error"
          });
        }
      },
      fail(res) {
        console.log(res);
        const result = {
          code: "50010",
          msg: "服务器出现错误！",
          data: {}
        }
        reject(result);
      }
    });
  },
  // 选择标签弹出层显示
  onLabelClick(event) {

    // event.detail 为当前输入的值
    console.log(event.detail);
    this.setData({
      popup_label_show: true,
    })
  },

  // 选择标签弹出层关闭
  onLabelClose() {
    this.setData({ popup_label_show: false });
  },

  onlabelCheckboxChange(event) {

   
    this.setData({
      label_value: event.detail,
    });
    this.setData({
      label_value: event.detail,
    });

    // console.log("选择标签数据", this.data.label_value)

  },

  // 查询岗位
  getPositionDatas() {
    var url = this.data.base_urlstr + "/query_position2";
    var data = {
      page: this.data.queryInfo.page,
      page_size: this.data.queryInfo.page_size,
      company: this.data.company_value,
      manager: this.data.manager_value,
      line: "",
      operation: "",
      job_name: "",
      register_time: []

    }
    var token = this.data.token
    wx.request({
      url,
      data,
      header: {
        "Authorization": token
      },
      dataType: "json",
      method: "POST",
      success: (res) => {
        var result = res.data
        console.log("岗位信息", result)

        if (result.code == 20000) {

          var reslit_list = result.data.results
          var data_list = []
          for (let i in reslit_list) {
            var data_arr = {
              position_keys: reslit_list[i].line + "-" + reslit_list[i].operation + "-" + reslit_list[i].job_name,
              key: reslit_list[i].key

            }

            data_list.push(data_arr)
            // console.log("岗位信息11",data_list)

          }
          // console.log("岗位信息",data_list)
          this.setData({
            position_list: data_list,
          })
        } else {
          wx.showToast({
            title: result.msg,
            icon: "error"
          });
        }
      },
      fail(res) {
        console.log(res);
        const result = {
          code: "50010",
          msg: "服务器出现错误！",
          data: {}
        }
        reject(result);
      }
    });
  },
  // 选择岗位弹出层显示
  onPositionClick(event) {

    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_position_show: true,
    })
  },
  // 选择岗位弹出层关闭
  onPositionClose() {
    this.setData({ popup_position_show: false });
  },
  onPositionCheckboxChange(event) {
    this.setData({
      position_value: event.detail,
    });
    this.setData({
      position_value: event.detail,
    });

    // console.log("选择岗位数据", this.data.position_value)

  },




  // 查询巷道灯
  getIndicatorDatas() {
    var url = this.data.base_urlstr + "/query_indicator";
    var data = {
      page: this.data.queryInfo.page,
      page_size: this.data.queryInfo.page_size,
      company: this.data.company_value,
      manager: this.data.manager_value,
      mac: "",
      supply: "",
      register_time: []

    }
    var token = this.data.token
    wx.request({
      url,
      data,
      header: {
        "Authorization": token
      },
      dataType: "json",
      method: "POST",
      success: (res) => {
        var result = res.data
        console.log("巷道灯信息", result)

        if (result.code == 20000) {
          this.setData({
            indicator_list: result.data.results,
          })
        } else {
          wx.showToast({
            title: result.msg,
            icon: "error"
          });
        }
      },
      fail(res) {
        console.log(res);
        const result = {
          code: "50010",
          msg: "服务器出现错误！",
          data: {}
        }
        reject(result);
      }
    });
  },
  // 选择巷道灯弹出层显示
  onIndicatorClick(event) {

    this.setData({
      popup_indicator_show: true,
    })
  },
  // 选择巷道灯弹出层关闭
  onIndicatorClose() {
    this.setData({ popup_indicator_show: false });
  },
  onIndicatorCheckboxChange(event) {
    this.setData({
      indicator_value: event.detail,
    });
    this.setData({
      indicator_value: event.detail,
    });

    // console.log("选择巷道灯数据", this.data.indicator_value)

  },


  // 查询平板
  getPadDatas() {
    var url = this.data.base_urlstr + "/query_pad";
    var data = {
      page: this.data.queryInfo.page,
      page_size: this.data.queryInfo.page_size,
      company: this.data.company_value,
      manager: this.data.manager_value,
      mac: "",
      supply: "",
      register_time: []

    }
    var token = this.data.token
    wx.request({
      url,
      data,
      header: {
        "Authorization": token
      },
      dataType: "json",
      method: "POST",
      success: (res) => {
        var result = res.data
        console.log("平板信息", result)

        if (result.code == 20000) {
          this.setData({
            pad_list: result.data.results,
          })
        } else {
          wx.showToast({
            title: result.msg,
            icon: "error"
          });
        }
      },
      fail(res) {
        console.log(res);
        const result = {
          code: "50010",
          msg: "服务器出现错误！",
          data: {}
        }
        reject(result);
      }
    });
  },
  // 选择平板弹出层显示
  onPadClick(event) {

    this.setData({
      popup_pad_show: true,
    })
  },
  // 选择平板弹出层关闭
  onPadClose() {
    this.setData({ popup_pad_show: false });
  },
  onPadCheckboxChange(event) {
    this.setData({
      pad_value: event.detail,
    });
    this.setData({
      pad_value: event.detail,
    });

    // console.log("选择平板数据", this.data.pad_value)

  },


  // 新增岗位绑定
  addBindPosition() {
    var url = this.data.base_urlstr + "/add_binds_position";
    var data = {
      company: this.data.company_value,
      manager: this.data.manager_value,
      label_keys: this.data.label_value,
      position_keys: this.data.position_value,
      indicator_keys: this.data.indicator_value,
      executor_keys: this.data.pad_value,

    }
    var token = this.data.token
    console.log("绑定数据", data)
    wx.request({
      url,
      data,
      header: {
        "Authorization": token
      },
      dataType: "json",
      method: "POST",
      success: (res) => {
        var result = res.data
        console.log("新增绑定返回信息", result)

        if (result.code == 20000) {
          this.setData({
            company_value: "",
            manager_value: "",
            label_value: "",
            position_value: "",
            indicator_value: "",
            pad_value: "",

          })
          wx.showToast({
            title: result.msg,
            icon: "success"
          });
        } else {
          wx.showToast({
            title: result.msg,
            icon: "error"
          });
        }
      },
      fail(res) {
        console.log(res);
        const result = {
          code: "50010",
          msg: "服务器出现错误！",
          data: {}
        }
        reject(result);
      }
    });
  },








  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({ login_show: true });


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})